package com.tuu.security;

import com.alibaba.fastjson.JSON;
import com.tuu.vo.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    //当用户无权限时时  走这步
    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException)
            throws IOException, ServletException {


        log.error("JwtAuthenticationEntryPoint不通过:"+authException.getMessage());
        //response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"没有凭证");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(ResultBody.error("500","权限不足")));
    }
}
